/*
 * @lc app=leetcode.cn id=504 lang=javascript
 *
 * [504] 七进制数
 */

// @lc code=start
/**
 * @param {number} num
 * @return {string}
 */
var convertToBase7 = function(num) {
  return convertToBaseN(num, 7)
};

function convertToBaseN(num, n) {
  if (num === 0) return '0'
  let res = []
  let negative = num < 0
  if (negative) num = Math.abs(num)
  while (num) {
    res.push(num % n)
    num = Math.floor(num / n)
  }
  if (negative) res.push('-')
  return res.reverse().join('')
}
// @lc code=end

